/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_client_callback_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleSsapClientCallbackStub::SleSsapClientCallbackStub()
{
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_FIND_SERVICE] =
        &SleSsapClientCallbackStub::OnFindServiceInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_FIND_PROPERTY] =
        &SleSsapClientCallbackStub::OnFindPropertyInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_FIND_COMPLETE] =
        &SleSsapClientCallbackStub::OnFindCompleteInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_WRITE_REASULT] =
        &SleSsapClientCallbackStub::OnWriteResultInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_READ_RESULT] =
        &SleSsapClientCallbackStub::OnReadResultInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_READ_BY_UUID_COMPLETE] =
        &SleSsapClientCallbackStub::OnReadByUuidCompleteInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_EXCHANGE_INFO] =
        &SleSsapClientCallbackStub::OnExchangeInfoInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_NOTIFICATION] =
        &SleSsapClientCallbackStub::OnNotificationInner;
    memberFuncMap_[SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_INDICATION] =
        &SleSsapClientCallbackStub::OnIndicationInner;
}

SleSsapClientCallbackStub::~SleSsapClientCallbackStub(){}

int SleSsapClientCallbackStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    if(code != SLE_SSAP_CLIENT_CALLBACK_WRITE_REASULT && code != SLE_SSAP_CLIENT_CALLBACK_NOTIFICATION
            && code != SLE_SSAP_CLIENT_CALLBACK_INDICATION) {
        SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    }
    std::u16string descriptor = SleSsapClientCallbackStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleSsapClientCallbackInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleSsapClientCallbackStub::OnFindServiceInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnFindServiceInner In");
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapClientFindServiceRes> result(data.ReadParcelable<SsapClientFindServiceRes>());
    SLE_ASSERT_AND_RETURN_RET_LOG(result, TRANSACTION_ERR, "read find service result failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnFindService(clientId, connectId, *result, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnFindPropertyInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnFindPropertyInner In");
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapClientFindPropertyRes> result(data.ReadParcelable<SsapClientFindPropertyRes>());
    SLE_ASSERT_AND_RETURN_RET_LOG(result, TRANSACTION_ERR, "read find property result failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnFindProperty(clientId, connectId, *result, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnFindCompleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnFindCompleteInner In");
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    uint8_t type;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(type), TRANSACTION_ERR, "read type failed!");

    std::shared_ptr<Uuid> uuid(data.ReadParcelable<Uuid>());
    SLE_ASSERT_AND_RETURN_RET_LOG(uuid, TRANSACTION_ERR, "read uuid failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnFindComplete(clientId, connectId, type, *uuid, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnWriteResultInner(MessageParcel &data, MessageParcel &reply)
{
#if 0
    SLE_LOGD("OnWriteResultInner In");
#endif
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");
    
    uint16_t handle;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(handle), TRANSACTION_ERR, "read write handle failed!");

    uint8_t type;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(type), TRANSACTION_ERR, "read write type failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnWriteResult(clientId, connectId, handle, type, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnReadResultInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnReadResultInner In");
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapClientHandleValue> value(data.ReadParcelable<SsapClientHandleValue>());
    SLE_ASSERT_AND_RETURN_RET_LOG(value, TRANSACTION_ERR, "read read result failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnReadResult(clientId, connectId, *value, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnReadByUuidCompleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnReadByUuidCompleteInner In");
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    uint8_t type;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(type), TRANSACTION_ERR, "read type failed!");

    std::shared_ptr<Uuid> uuid(data.ReadParcelable<Uuid>());
    SLE_ASSERT_AND_RETURN_RET_LOG(uuid, TRANSACTION_ERR, "read uuid failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnReadByUuidComplete(clientId, connectId, type, *uuid, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnExchangeInfoInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnExchangeInfoInner In");
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    uint32_t mtu;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(mtu), TRANSACTION_ERR, "read mtu failed!");

    uint16_t version;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(version), TRANSACTION_ERR, "read version failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnExchangeInfo(clientId, connectId, mtu, version, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnNotificationInner(MessageParcel &data, MessageParcel &reply)
{
#if 0
    SLE_LOGD("OnNotificationInner In");
#endif
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapClientHandleValue> value(data.ReadParcelable<SsapClientHandleValue>());
    SLE_ASSERT_AND_RETURN_RET_LOG(value, TRANSACTION_ERR, "read notification value failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnNotification(clientId, connectId, *value, errCode);
    return NO_ERROR;
}

int SleSsapClientCallbackStub::OnIndicationInner(MessageParcel &data, MessageParcel &reply)
{
#if 0
    SLE_LOGD("OnIndicationInner In");
#endif
    uint8_t clientId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(clientId), TRANSACTION_ERR, "read client id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapClientHandleValue> value(data.ReadParcelable<SsapClientHandleValue>());
    SLE_ASSERT_AND_RETURN_RET_LOG(value, TRANSACTION_ERR, "read indication value failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnIndication(clientId, connectId, *value, errCode);
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS